//
//  MoyaAPI.swift
//  SwiiftProject
//
//  Created by 圣光大人 on 2019/5/10.
//  Copyright © 2019 lazyloading. All rights reserved.
//

import Foundation
import Moya


let baseUrl = "http://47.93.203.21/ruby-api"


//MARK: 🐷 接口 🐷
enum MoyaAPI {
    ///注册
    case register(_ : Dictionary<String ,Any>)
    ///登录
    case login(_ : Dictionary<String ,Any>)
    ///银行卡列表
    case bankList(_ : Dictionary<String ,Any>)
    ///添加银行卡
    case addBankCard(_ : Dictionary<String ,Any>)
    ///删除
    case deleteBankCard(_ : Dictionary<String ,Any>)

    ///首页公告
    case notice
    ///二维码列表
    case qrCodeList(_ : Dictionary<String ,Any>)
    ///添加二维码
    case addCode(_ : Dictionary<String,Any>)
    ///删除二维码
    case deleteQRCode(_ : Dictionary<String ,Any>)
    ///订单列表
    case orderList(_ : Dictionary<String,Any>)
    ///接单
    case receiveOrder(_ : Dictionary<String,Any>)
    ///上传图片
    case uploadData(_ : [UIImage])
    ///充值
    case recharge(_ : Dictionary<String,Any>)
    ///获取充值账户
    case getRechargeInfo
    ///获取用户信息
    case userInfo
    ///客服
    case  customer
    ///获取邀请码
    case inviteCode
}


//MARK: 🐷 接口配置 🐷
extension MoyaAPI : TargetType{
    
    var baseURL: URL {
        return URL.init(string: baseUrl)!
    }

    var path: String {
        switch self {
            
        case.register(param: _):
            return "/register";
            
        case .login(param : _):
            return "/login";
            
        case .bankList(param : _):
            return "/card/list";
            
        case .addBankCard(param : _):
            return "/card/add";
            
        case .notice:
            return "/notice";
            
        case .qrCodeList:
            return "/portal/paymentmethod/list";
            
        case .addCode:
            return "/portal/paymentmethod/add";
            
        case .deleteBankCard:
            return "/card/delete";
            
            
        case .deleteQRCode:
            return "/portal/paymentmethod/delete";
            
        case .orderList:
            return "/order/list/all";
            
        case .receiveOrder:
            return "/order/receive";
            
        case  .uploadData:
            return "/file/upload";
            
        case .recharge:
            return "/charge";
            
        case .getRechargeInfo:
            return "/sys/data/payInfo";
            
            
        case .userInfo:
            return "/user";
            
            
        case .customer:
            return "/sys/data/csContact";
            
        case .inviteCode:
            return "/inviteCode";
            
            
        }
        
        
        
    }
    
    var method: Moya.Method {
        switch self {
        case .login(param: _):
            return .post
            
        case .register(param: _):
            return .post
            
        case .bankList(param: _):
            return .post
            
        case .addBankCard(param: _):
            return .post
            
        case .deleteBankCard(param: _):
            return .post;
            
        case .deleteQRCode(param: _):
            return .post;
            
        case .notice:
            return .post;
            
        case .qrCodeList(param: _):
            return .post;
            
        case .addCode(param: _):
            return .post;
            
        case .orderList(param: _):
            return .post;
            
        case .receiveOrder(param: _):
            return .post;
            
        case  .uploadData:
            return .post;
            
        case .recharge:
            return .post;
            
        case .getRechargeInfo:
            return .get;
            
        case .userInfo:
            return .get;
            
        case .customer:
            return .get;
            
        case .inviteCode:
            return .get;
            
        }
        
    }
    
    var sampleData: Data {
        return Data()
    }
    
    var task: Task {
        switch self {
        case .login(let param):
            return .requestParameters(parameters: param, encoding: JSONEncoding.default)
            
        case .register(let param):
            return .requestParameters(parameters: param, encoding: JSONEncoding.default)
            
        case .bankList(let param):
            return .requestParameters(parameters: param, encoding: JSONEncoding.default)
            
        case .addBankCard(let param):
            return .requestParameters(parameters: param, encoding: JSONEncoding.default)
            
        case .deleteBankCard(let param):
            return .requestParameters(parameters: param, encoding: JSONEncoding.default)
            
        case .deleteQRCode(let param):
            return .requestParameters(parameters: param, encoding: JSONEncoding.default)
            
        case .notice:
            return .requestParameters(parameters: ["" : ""], encoding: JSONEncoding.default)
            
        case .qrCodeList(let param):
            return .requestParameters(parameters: param, encoding: JSONEncoding.default)
            
        case .addCode(let param):
            return .requestParameters(parameters: param, encoding: JSONEncoding.default)
            
        case .orderList(let param):
            return .requestParameters(parameters: param, encoding: JSONEncoding.default)
            
        case .receiveOrder(let param):
            return .requestParameters(parameters: param, encoding: JSONEncoding.default)
            
        case .uploadData(let dataAry):
            
            let image = dataAry.first;
            let date:Date = Date()
            let formatter = DateFormatter()
            formatter.dateFormat = "yyyy-MM-dd-HH:mm:ss"
            var dateStr:String = formatter.string(from: date as Date)
            dateStr = dateStr.appendingFormat("-%i.png", 1)
            let imageData = image?.jpegData(compressionQuality: 0.8)
            let formData: [Moya.MultipartFormData] = [Moya.MultipartFormData(provider: .data(imageData!), name: "file", fileName: dateStr, mimeType: "image/jpeg")]
            
            return .uploadMultipart(formData)
            
            
        case .recharge(let param):
            return .requestParameters(parameters: param, encoding: JSONEncoding.default)

        case .getRechargeInfo:
            return .requestPlain
            
        case .userInfo:
            return .requestPlain
            
        case .customer:
            return .requestPlain
            
        case .inviteCode:
            return .requestPlain


        default:
            return .requestParameters(parameters: ["" : ""], encoding: JSONEncoding.default)
        }
    }
    
    var headers: [String : String]? {
        
        switch self {
        default:
            return ["Content-Type": "application/json","token" : UserinfoModel.getInfo()?.token ?? ""];
        }
    }
    
    
}
